package com.yc.spring.jdbc.bank.dao;

import com.yc.spring.jdbc.bank.bean.Record;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class RecordDao {
    @Resource
    JdbcTemplate jdbcTemplate;
    public int insert(long accountid, double opmoney) {
        String sql = "insert into oprecord (accountid,opmoney)" +
                " values(?,?)";
        return jdbcTemplate.update(sql, accountid, opmoney);
    }
    public List<Record> select(long accountid) {
        String sql = "select * from oprecord where accountid=?";
        return jdbcTemplate.query(sql, (rs, index) -> {
            // rs 是结果集 Resultset, index 当前行号
            Record record = new Record();
            record.setId(rs.getLong("id"));
            record.setAccountid(rs.getLong("accountid"));
            record.setOpmoney(rs.getDouble("opmoney"));
            return record;
        }, accountid);
    }

}
